import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * The Story class runs the model of the game. It contains the Ship and the
 * different States, as well as the Timeline, and it progresses the ship through
 * these different states and communicates with the Display class to show all of
 * this to the player.
 */
public class Story implements PropertyChangeListener
{
    private Ship ship;
    private HUD hud;
    private Display display;
    private Shop shop;
    private Timeline timeline;
    private SaveLoad Saver;

    public Story()
    {
        ship = new Ship();
        timeline = new Timeline(ship);
        	timeline.addStoryListener(this);  //all that is necessary for property change support - ***nothing more
        hud = new HUD(ship, timeline);
        	hud.addStoryListener(this);
        	hud.updateHUD(0);
        display = new Display();
            display.setHUD(new TitleSplash());
            display.start();
        
        display.changeState(timeline.beginState());
    }

    /**
     * Kills the state
     * @param reason Why it did it
     */
    public void terminate(String reason){
    	JPanel terminatePanel = new JPanel();
    	terminatePanel.add(new JLabel(reason));
    	
    	display.changeState(terminatePanel);
    }
    
    /**
     * Goes to the next Step
     */
    public void nextStep()
    {
    	//changes the HUD from splash panel for titlescreen to operational game HUD
    	if (timeline.getCurrentStep()==1)
    		display.setHUD(hud);
    	
        display.changeState(timeline.nextState());  //timeline method nextState() starts the incoming state
    }

    /**
     * This method is called when certain classes, most likely State classes,
     * fire a property change, usually being finished. This method is then used
     * to change the display from one state to another.
     */
    @Override
    public void propertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName() == "finished"){
        	nextStep();
        	hud.updateHUD(0);
        }
        else if (evt.getPropertyName() == "finished Sailing"){
        	nextStep();
        	hud.updateHUD(1);
        }
        else if (evt.getPropertyName() == "inventory") hud.updateHUD(0);
        else if (evt.getPropertyName() == "die-hunger") terminate("sorry, you died from hunger");
        else if (evt.getPropertyName() == "die-thurst") terminate("sorry, you died from thirst");
        else if (evt.getPropertyName() == "PirateKill") terminate("sorry, the pirate sank your ship");
        else if (evt.getPropertyName() == "storm") terminate("sorry, a storm sank your ship");
        else if (evt.getPropertyName() == "sailAround") {
        	timeline.sailAround();
        	nextStep();
        	hud.updateHUD(0);
        }
        else if (evt.getPropertyName() == "load"){
        	int startStep = timeline.getCurrentStep()-1;
        	System.out.println("Attempting to load game from step: " +startStep);
        	timeline.setCurrentStep(startStep);
        	nextStep();
        }
    }
}